import { CreateEdgeInteractor } from 'common/topo/CreateEdgeInteractor.js'
import topoApi from 'api/topo'
import { Message } from 'element-ui';
import { MessageBox } from 'element-ui';
export default {
    // 新增连接
    topoAddLink(graphView, sysmapid, message) {
        let edgeInteractor = new CreateEdgeInteractor(graphView);
        const dataModel = graphView.getDataModel();
        graphView.setInteractors([
            new ht.graph.DefaultInteractor(graphView),
            new ht.graph.TouchInteractor(graphView, {
                selectable: false
            }),
            edgeInteractor
        ]);

        // const sysmapid = self.$store.state.topo.sysmapid;
        var eventType = ht.Default.isTouchable ? 'touchend' : 'mouseup';
        graphView.getView().addEventListener(eventType, function(e) {
            // e.preventDefault();
            e = e || window.event;
            if (edgeInteractor != null) {
                const source = edgeInteractor._sourceNode;
                const target = edgeInteractor._targetNode;
                if (source != null && target != null) {
                    let linkData = {};
                    linkData.sysmapid = sysmapid;
                    linkData.selementid1 = source._id;
                    linkData.selementid2 = target._id;
                    linkData.drawtype = "0";
                    linkData.color = "00CC00";
                    linkData.label = '';
                    topoApi.addMapLink(linkData).then(response => {
                        const newLinkData = response.data;

                        var link = new ht.Edge(); //创建线,设置id
                        link.setId(newLinkData.linkid);
                        link.setAttr("data", newLinkData); //线的各种数据都存入线中
                        link.setSource(source);
                        link.setTarget(target);
                        link.setStyle("edge.color", '#' + newLinkData.color);
                        link.setName(newLinkData.label);
                        dataModel.add(link); //添加到网元容器中

                        graphView.setEditable(false);
                        edgeInteractor = null;

                        // self.$message({type:'success', message: message.success});
                        Message.success(message.success);
                    }).catch(err => {
                        // self.$message({type:'error', message: message.failed});
                        Message.error(message.failed);
                    });

                } else {
                    // self.$message(self.$t('Failed to create'));
                }
            }
        });
    },

    // 删除拓扑图元素
    deleteMapElement(graphView, message) {
        const dataModel = graphView.getDataModel();
        let eles = dataModel.getSelectionModel().getSelection()
        let currentMapElementData = null
        let currentNodeOrLink = null
        if (eles != null && eles.length > 0) {
            currentNodeOrLink = eles.get(0);
            if (currentNodeOrLink) {
                try {
                    currentMapElementData = currentNodeOrLink._attrObject.data;
                } catch (e) {
                    currentMapElementData = null
                }
            }
        }
        if (!currentMapElementData) {
            Message.error({ message: '请选择要删除的元素' });
            return
        }
        const confirmInfo = "是否删除元素: " + currentMapElementData.label;
        MessageBox.confirm(confirmInfo, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
        }).then(() => {
            if (currentMapElementData) {
                if (currentMapElementData.selementid) { // 节点
                    topoApi.deleteMapElement(currentMapElementData.selementid).then(response => {
                        if (!response.data) {
                            Message.error(message.failed);
                        } else {
                            Message.success(message.success);
                            // 删除显示
                            dataModel.remove(currentNodeOrLink);
                        }
                    }).catch(err => {
                        Message.error(message.failed);
                    });
                } else if (currentMapElementData.linkid) { // 连接
                    topoApi.deleteMapLink(currentMapElementData.linkid).then(response => {
                        if (!response.data) {
                            Message.error(message.failed);
                        } else {
                            Message.success(message.success);
                            dataModel.remove(currentNodeOrLink);
                        }
                    }).catch(err => {
                        Message.error(message.failed);
                    });
                }
            }
        }).catch(() => {
            Message.info(message.info);
        });
    }
}